package com.cosina.game.crystallight.ui.battle;

import java.util.HashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.cosina.game.crystallight.model.battle.Ability;
import com.cosina.game.crystallight.ui.ResourceManager;
import com.cosina.game.crystallight.ui.common.PaintSuite;
import com.cosina.game.crystallight.util.BitmapUtil;
import com.cosina.game.crystallight.util.CanvasUtil;
import com.cosina.game.crystallight.util.World;

public class BattleResource {
	public static Bitmap 
	ram1,ram2,ram3,manabarBorder,
	enemyHome,castleH,castleV,msShield,
	tower, puddle, 
	manaBar,
	victory,defeat;

	public static Bitmap[] 
         digital,newWaveFrames,shot,hit,explosion,vortex,genCrystalBitmaps;
	
	public static Bitmap []  overlay = new Bitmap[12];
	
	static Map<Integer,Bitmap> bitmaps = new HashMap<Integer,Bitmap>();
	
	
	private static Bitmap[][] tilesets = new Bitmap[3][16];
	
	
	public static Bitmap getTile(int mapType, int tileType){
		return tilesets[mapType][tileType];
	}
	
	public static void init(){
		CrystalResouce.init();
		{
			for(int typeIndex=0; typeIndex<3; typeIndex++){
				Bitmap bitmap = ResourceManager.get("img/cardinal/tileset_" + typeIndex + ".jpg");
				for(int i=0; i<16; i++){
					tilesets[typeIndex][i] = Bitmap.createBitmap(bitmap, i%4*44, 44*(i/4), 44, 44);
				}
			}
		}
		
		for(int i=1; i<12; i++){
			overlay[i] = ResourceManager.get("img/cardinal/ovlaps/o_" + i + ".png");
		}
		
		victory = ResourceManager.getNoCahce("img/cardinal/victory.png");
		defeat = ResourceManager.getNoCahce("img/cardinal/defeat.png");
		ram1 = ResourceManager.get("img/cardinal/ram1.jpg");
		ram2 = ResourceManager.get("img/cardinal/ram2.jpg");
		ram3 = ResourceManager.get("img/cardinal/ram3.jpg");
		manabarBorder = ResourceManager.get("img/cardinal/ctrl_panel.jpg");
		enemyHome = ResourceManager.get("img/cardinal/source.png");
		castleH = ResourceManager.get("img/cardinal/drain_h.png");
		castleV = ResourceManager.get("img/cardinal/drain_v.png");
		msShield = ResourceManager.get("img/cardinal/mshield.png");
		tower =  ResourceManager.get("img/cardinal/tower.png");
		puddle =  ResourceManager.get("img/cardinal/puddle.png");
		vortex = BitmapUtil.cut(ResourceManager.getNoCahce("img/cardinal/vortex.png"), 6, -1);
		manaBar = ResourceManager.get("img/cardinal/mana_pb.jpg");
		bitmaps.put(Ability.MINE, ResourceManager.get("img/cardinal/btn_gemmine.png"));
		bitmaps.put(Ability.MIXTURE, ResourceManager.get("img/cardinal/btn_main.jpg"));
		regBooleanImage(Ability.TOWER,"img/cardinal/btn_tower.png");
		regBooleanImage(Ability.PUDDLE,"img/cardinal/btn_puddle.png");
		regBooleanImage(Ability.VORTEX,"img/cardinal/btn_vortex.png");
		regBooleanImage(Ability.RANGE,"img/cardinal/btn_inc_r.png");
		regBooleanImage(Ability.SPEED,"img/cardinal/btn_inc_fspeed.png");
		regBooleanImage(Ability.ACCERLATION,"img/cardinal/btn_speed.png");
		regBooleanImage(Ability.GENM,"img/cardinal/btn_gem.jpg");
		
		{
			genCrystalBitmaps = cut("img/cardinal/gem_colors.jpg", 4);
			newWaveFrames = claerAndcut("img/cardinal/crystals/ps_upg.jpg", 16);
			shot = claerAndcut("img/cardinal/crystals/shot0.jpg", 4);
			hit = claerAndcut("img/cardinal/crystals/hit.jpg", 2);
			explosion = cut("img/cardinal/monsters/ps_exp.png", 10);
			digital = cut("img/cardinal/crystals/font.png", 10);
		}
	}
	
	private static Bitmap[] cut(String path, int part){
		Bitmap someBitmap = ResourceManager.getNoCahce(path);
		return BitmapUtil.cut(someBitmap, part, -1);
	}
	
	private static Bitmap[] claerAndcut(String path, int part){
		Bitmap someBitmap = ResourceManager.getNoCahce(path);
		someBitmap = BitmapUtil.clearBlack(someBitmap);
		return BitmapUtil.cut(someBitmap, part, World.V);
	}

	public static Bitmap createWavePanelImage(int totalWave){
		Bitmap result = Bitmap.createBitmap(25, 38*totalWave, Bitmap.Config.ARGB_8888);
		
		Bitmap singlePanel=  ResourceManager.get("img/cardinal/wave_panel.jpg");
		
		Paint paint = new Paint(PaintSuite.p4Text);
		Canvas canvas = new Canvas(result);
		int redFactor = 0;
		int greenFactor = 255;
		
		int adjustFactor = 255/ totalWave;
		
		for(int i = 1; i< totalWave + 1; i++){
			canvas.drawBitmap(singlePanel, 0, i*38 -38, PaintSuite.p4Simple);
			
			paint.setColor(Color.rgb(redFactor, greenFactor, 0));
			CanvasUtil.drawText(canvas, i, 12, i*38-22, paint, 16);

			redFactor += adjustFactor;
			greenFactor -= adjustFactor;
		}
		return result;
	}
	
	private static void regBooleanImage(int id, String path){
		Bitmap bitmap = ResourceManager.get(path);
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		bitmaps.put(id,Bitmap.createBitmap(bitmap, 0,0, width/2, height));
		bitmaps.put(-id,Bitmap.createBitmap(bitmap, width/2,0, width/2, height));
	}
	
	public static Bitmap get(int tileType){
		return bitmaps.get(tileType);
	}
}
